/**
 * Copyright 2008 Benjamin Possolo
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.javenue.util.process.support;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.javenue.util.process.Process;
import org.javenue.util.process.ProcessState;

/**
 * @author Benjamin Possolo
 * Jul 9, 2008
 */
public class ProcessStateHelper {

	//-----------------------------------------------------
	//Public methods
	//-----------------------------------------------------
	public static void setField(ProcessState processState, String fieldName, Object value)
		throws NoSuchFieldException, IllegalAccessException {
		
		setField(processState.getClass(), processState, fieldName, value);
	}
	
	public static ProcessState getProcessState(Process<? extends ProcessState> process) {
		
		try{
			Method m = Process.class.getDeclaredMethod("getProcessState", new Class[0]);
			m.setAccessible(true);
			
			return (ProcessState)m.invoke(process, new Object[0]);
		}
		catch(NoSuchMethodException e){ //this should never happen
			throw new RuntimeException("Process class does not define getProcessState() method", e);
		}
		catch(IllegalAccessException e){ //this should never happen
			throw new RuntimeException("getProcessState() method invocation disallowed", e);
		}
		catch(InvocationTargetException e){ //this should never happen
			throw new RuntimeException("Unable to invoke getProcessState() method on Process object", e);
		}
	}
	
	public static void setProcessState(Process<? extends ProcessState> process, ProcessState processState){
		
		try{
			Method m = Process.class.getDeclaredMethod("setProcessState", new Class[] { ProcessState.class });
			m.setAccessible(true);
			
			m.invoke(process, new Object[] { processState });
		}
		catch(NoSuchMethodException e){ //this should never happen
			throw new RuntimeException("Process class does not define getProcessState() method", e);
		}
		catch(IllegalAccessException e){ //this should never happen
			throw new RuntimeException("setProcessState(ProcessState) method invocation disallowed", e);
		}
		catch(InvocationTargetException e){ //this should never happen
			throw new RuntimeException("Unable to invoke setProcessState(ProcessState) method on Process object", e);
		}
	}
	
	//-----------------------------------------------------
	//Private methods
	//-----------------------------------------------------
	private static void setField(Class<?> clazz, ProcessState processState, String fieldName, Object value)
		throws NoSuchFieldException, IllegalAccessException {

		try {
			Field f = clazz.getDeclaredField(fieldName);
			f.setAccessible(true);
			f.set(processState, value);
		}
		catch(NoSuchFieldException e) {
			if( clazz.getSuperclass() == null )
				throw e;
			else
				setField(clazz.getSuperclass(), processState, fieldName, value);
		}
	}
	
	

}
